package org.myname.example1;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




/**
 * Servlet implementation class Servlet1
 */
@WebServlet(
		urlPatterns = { "/Servlet1" }, 
		initParams = { 
				@WebInitParam(name = "config", value = "abcde"),
				@WebInitParam(name = "config1", value = "yaya")
		})
public class Servlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private ServletConfig config;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config=config;
	}

	public ServletConfig getConfig() {
		return config;
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
//		ServletConfig config2=this.getServletConfig();
//		Enumeration<String> enums=request.getParameterNames();
//		while(enums.hasMoreElements()){
//			String name=enums.nextElement();
//			String values=request.getParameter(name);
//			response.getWriter().append("name:"+name+",values:"+values+"<br>");
//		}
//		response.getWriter().append("================================").append("<br>");
//        doGet(request,response);
//		response.sendRedirect("https://www.baidu.com");
//		response.sendError(404);
		
		throw new NullPointerException("null pointer");
	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath()).append("<br>");
//		response.getWriter().append("Req Char Encoding: ").append(request.getCharacterEncoding()).append("<br>");
//		response.getWriter().append("Req Content Type: ").append(request.getContentType()).append("<br>");
//		response.getWriter().append("Req Content Type: ").append(String.valueOf(request.getContentLength())).append("<br>");
//		response.getWriter().append("Req Type: ").append(request.getClass().getName()).append("<br>");
//		
//		response.setContentType("image/jpg");
//		String file ="C:\\Users\\qingke\\Desktop\\res03_attpic_brief.jpg";
//		FileInputStream fis =new FileInputStream(new File(file));
//		int length =fis.available();
//		byte[] content =new byte[length];
//		fis.read(content);
//		fis.close();
//		response.getOutputStream().write(content);
//		response.setContentLength(length);
//		response.flushBuffer();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
